/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-6 上午9:29
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.bean.MyInfo;

import java.util.ArrayList;
import java.util.List;

/**
*description:消息Adapter
*autour:guanluocang
*date:17/7/17 09:54
*/
public class MyInfoAdapter extends RecyclerView.Adapter<MyInfoAdapter.MyInfoViewHolder> {
    private Context mContext;
    private List<MyInfo.DataBean> mInfoList = new ArrayList<>();
    private LayoutInflater mLayoutInflater;
    private onItemClickListener onItemClickListener;

    public MyInfoAdapter(Context context) {
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void refresh_info(List<MyInfo.DataBean> infoList) {
        this.mInfoList = infoList;
        notifyDataSetChanged();
    }

    //点击事件和长按事件
    public interface onItemClickListener {
        void onItemClick(View view, int position);

    }
    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    @Override
    public MyInfoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyInfoViewHolder viewHolder = new MyInfoViewHolder(mLayoutInflater.inflate(R.layout.layout_myinfo_item,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyInfoViewHolder holder, int position) {
        holder.tv_infoTitle.setText(mInfoList.get(position).getTitle());
        holder.tv_infoTime.setText(mInfoList.get(position).getAdd_time());
        holder.tv_infoContent.setText(mInfoList.get(position).getContent());
        if(onItemClickListener != null){
            holder.ll_infoAdapterItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mInfoList.size();
    }

    class MyInfoViewHolder extends RecyclerView.ViewHolder {

        LinearLayout ll_infoAdapterItem;
        TextView tv_infoTitle;
        TextView tv_infoTime;
        TextView tv_infoContent;
        public MyInfoViewHolder(View itemView) {
            super(itemView);
            ll_infoAdapterItem = (LinearLayout) itemView.findViewById(R.id.ll_infoAdapterItem);
            tv_infoTitle = (TextView) itemView.findViewById(R.id.tv_infoTitle);
            tv_infoTime = (TextView) itemView.findViewById(R.id.tv_infoTime);
            tv_infoContent = (TextView) itemView.findViewById(R.id.tv_infoContent);
        }
    }
}
